﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    public class ReferralPatientDal
    {
        public DapperHelper dapperHelper { get; set; }


        /// <summary>
        /// 查询转诊关系表
        /// </summary>
        /// <param name="modifiedOn">修改时间</param>
        /// <param name="skipNums">跳过记录数</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns></returns>
        public IEnumerable<dynamic> QueryReferralShipPageByModifiedOn(DateTime modifiedOn, int skipNums, int pageSize)
        {
            string sqlstr = @"SELECT      [leadid]
                                          ,[referral_outoa]
                                          ,[referral_outname]
                                          ,[referral_outemedicalhospitalid]
                                          ,[referral_outhospitalname]
                                          ,[referral_incrm]
                                          ,[referral_inoa]
                                          ,[referral_inname]
                                          ,[referral_inemedicalhospitalid]
                                          ,[referral_inhospitalname]
                                          ,[modifiedon]
                                          ,[createdon]
                                          ,[description]
                                          ,[new_patientsource]
                                          ,[new_disease]
                                          ,[isfollow]
                                          ,[followdate]
                                          ,[identifydoctor]
                                          ,[expectedoperation]
                                          ,[safetydemands]
                                          ,[pricesensitivity]
                                          ,[benefitsavailable]
                                       FROM [dbo].[t_patientreferral] as p /**where**/ /**orderby**/";
            return dapperHelper.ExecueWithoutTransaction(a => {
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sqlstr);
                sqlBuilder.Where(@" p.modifiedon > @modifiedOn", new { modifiedOn = modifiedOn });
                sqlBuilder.OrderBy(@" p.createdon OFFSET @skipNums ROWS FETCH NEXT @pageSize ROWS ONLY",
                    new { skipNums = skipNums, pageSize = pageSize });
                return a.Query<dynamic>(sqlTemplate.RawSql, sqlTemplate.Parameters);
            });
        }

        /// <summary>
        /// 插入转诊关系表
        /// </summary>
        /// <param name="referralShip"></param>
        /// <returns></returns>
        public Int64 SaveReferralPatient(ReferralShip referralShip)
        {
            string sqlstr = @"INSERT INTO [dbo].[t_patientreferral]
                                       ([leadid]
                                       ,[referral_outoa]
                                       ,[referral_outname]
                                       ,[referral_outemedicalhospitalid]
                                       ,[referral_outhospitalname]
                                       ,[referral_incrm]
                                       ,[referral_inoa]
                                       ,[referral_inname]
                                       ,[referral_inemedicalhospitalid]
                                       ,[referral_inhospitalname]
                                       ,[modifiedon]
                                       ,[createdon]
                                       ,[statecode]
                                       ,[description]
                                       ,[new_patientsource]
                                       ,[new_disease]
                                       ,[isfollow]
                                       ,[followdate]
                                       ,[identifydoctor]
                                       ,[expectedoperation]
                                       ,[safetydemands]
                                       ,[pricesensitivity]
                                       ,[benefitsavailable])
                                 VALUES
                                       (@leadid
                                       ,@referral_outoa
                                       ,@referral_outname
                                       ,@referral_outemedicalhospitalid
                                       ,@referral_outhospitalname
                                       ,@referral_incrm
                                       ,@referral_inoa
                                       ,@referral_inname
                                       ,@referral_inemedicalhospitalid
                                       ,@referral_inhospitalname
                                       ,@modifiedon
                                       ,@createdon
                                       ,@statecode
                                       ,@description
                                       ,@new_patientsource
                                       ,@new_disease
                                       ,@isfollow
                                       ,@followdate
                                       ,@identifydoctor
                                       ,@expectedoperation
                                       ,@safetydemands
                                       ,@pricesensitivity
                                       ,@benefitsavailable);";

            sqlstr += "SELECT CAST(SCOPE_IDENTITY() as int)";

            return dapperHelper.ExecueWithoutTransaction<Int64>(a => {
                SqlBuilder sqlbuilder = new SqlBuilder();
                var sqlTemplate = sqlbuilder.AddTemplate(sqlstr);
                return a.Query<Int64>(sqlTemplate.RawSql, referralShip).FirstOrDefault();;
            });
        }
    }
}
